package com.gdssoft.pdaweb.action.water;
/*
 *  捷达世软件（深圳）有限公司
 *  Copyright (C) 2008-2010 ShenZhen JieDaShi Software Limited Company.
*/
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.gdssoft.core.command.QueryFilter;
import com.gdssoft.core.util.BeanUtil;
import com.gdssoft.core.util.ContextUtil;
import com.gdssoft.core.util.DateUtil;
import com.gdssoft.core.web.action.BaseAction;
import com.gdssoft.framework.model.system.AppUser;
import com.gdssoft.framework.service.system.DepartmentService;
import com.gdssoft.pdaweb.model.PdaLog;
import com.gdssoft.pdaweb.model.water.PdaWaterMeter;
import com.gdssoft.pdaweb.service.PdaLogService;
import com.gdssoft.pdaweb.service.water.PdaWaterMeterService;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

import flexjson.JSONSerializer;
/**
 * 
 * @author 
 *
 */
public class PdaWaterMeterAction extends BaseAction{
	@Resource
	private PdaWaterMeterService pdaWaterMeterService;
	@Resource
	private PdaLogService pdaLogService;
	@Resource
	private DepartmentService departmentService;
	
	private PdaWaterMeter pdaWaterMeter;
	
	private Long meterId;

	public Long getMeterId() {
		return meterId;
	}

	public void setMeterId(Long meterId) {
		this.meterId = meterId;
	}

	public PdaWaterMeter getPdaWaterMeter() {
		return pdaWaterMeter;
	}

	public void setPdaWaterMeter(PdaWaterMeter pdaWaterMeter) {
		this.pdaWaterMeter = pdaWaterMeter;
	}

	/**
	 * 显示列表
	 */
	public String list(){
		String depIds = departmentService.getSubDepIds();
		if(!depIds.isEmpty()){
			QueryFilter filter=new QueryFilter(getRequest());
			filter.addFilter("Q_department.depId_S_LIN", depIds);
			List<PdaWaterMeter> list= pdaWaterMeterService.getAll(filter);
			
			JSONSerializer serializer=new JSONSerializer();
			StringBuffer buff = new StringBuffer("{success:true,'totalCounts':")
			.append(filter.getPagingBean().getTotalItems()).append(",result:");		
	        buff.append(serializer.serialize(list));
			buff.append("}");
			
			jsonString=buff.toString();
		}else{
			jsonString="{success:false}";
		}
		
		return SUCCESS;
	}
	
	/**
	 * 显示详细信息
	 * @return
	 */
	public String get(){
		PdaWaterMeter pdaWaterMeter=pdaWaterMeterService.get(meterId);
		
		Gson gson=new GsonBuilder().setDateFormat("yyyy-MM-dd").create();
		//将数据转成JSON格式
		StringBuffer sb = new StringBuffer("{success:true,data:");
		sb.append(gson.toJson(pdaWaterMeter));
		sb.append("}");
		setJsonString(sb.toString());
		
		return SUCCESS;
	}
	/**
	 * 添加及保存操作
	 */
	public String save(){
		String opt = getRequest().getParameter("opt");
		String choose = getRequest().getParameter("choose");
		String selecteds = getRequest().getParameter("selected");
		
		if(pdaWaterMeter.getAmId()!=null){
			PdaWaterMeter orgPdaWaterMeter = pdaWaterMeterService.getByMeterId(pdaWaterMeter.getAmId());
			try{
				BeanUtil.copyNotNullProperties(orgPdaWaterMeter, pdaWaterMeter);
				pdaWaterMeter = orgPdaWaterMeter;
				pdaWaterMeter.setLasteditby(ContextUtil.getCurrentUser().getUsername());
				pdaWaterMeter.setLasteditdt(new Date().toString());
			}catch(Exception ex){
				logger.error(ex.getMessage());
			}
		}
		try {
			pdaWaterMeterService.save(pdaWaterMeter);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		if(!opt.isEmpty() && opt.equals("CHANGEPLAN")){
			pdaWaterMeterService.changePlan(choose,selecteds);
		}
		setJsonString("{success:true}");
		return SUCCESS;
	}
	 /**
	   * 暂停与恢复
	   */
		public String pause(){
			AppUser appUser = ContextUtil.getCurrentUser();
			String recId= getRequest().getParameter("recId");
			String vType= getRequest().getParameter("vType");
			String dateT = DateUtil.dateFormat(new Date(), "yyyy-MM-dd");
			if(pdaWaterMeterService.pauseAndResume(recId, vType, appUser, dateT)){
				PdaLog pdaLog=new PdaLog();
				if(vType.equals("2")){
				pdaLog.setAction(appUser.getUsername()+"于"+dateT+"暂停水表"+recId);
				}else{
				pdaLog.setAction(appUser.getUsername()+"于"+dateT+"恢复水表"+recId);
				}
				pdaLog.setLogType((short) 0);
				pdaLog.setUserId(appUser.getUsername());
				pdaLog.setOperateTime(new Date());
				try {
					pdaLogService.save(pdaLog);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					logger.error(e.getMessage());
				}			
			}		
			setJsonString("{success:true}");
			return SUCCESS;
		}
		
}
